// pages/activity_info/activity_info.js
var wxParse = require('../../wxParse/wxParse.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    act_info: {},
    agentList: [],
    user_list: [],
    goods_info: {},
    goodDescription: "", //活动详情
    base_url:'https://vip.join.ink/',
    activity_content:''
  },

  join() {
    let id = this.data.act_info.id
    wx.navigateTo({
      url: '/pages/create_order_act/create_order_act?id='+id,
    })
  },

  fetch(id) {
    var  that   =this
    app.request("/getac_detail", {
      data: {
        id
      }
    }).then(res => {
  
      res.data.data[0].acitivity_desc.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match, capture) {         
        if(capture.indexOf("https")>=0 || capture.indexOf("http")>=0 ){
          var desc  =  res.data.data[0].acitivity_desc;
          console.log('包含此字符串')
        }else{
          console.log('不包含此字符串')
         var desc =  res.data.data[0].acitivity_desc.replace(/src="/gi, 'src="https://vip.join.ink')
        }
        that.setData({
          activity_content:desc
        })
      });
      this.setData({
        act_info:res.data.data[0],
      })
       var  datas = res.data.data[0]
    // wxParse.wxParse("goodDescription", "html", that.data.act_info.acitivity_desc, that, 5);
    })
  },
  //校验缓存中的token 和 通过openid拿到的token进行比对
check_token() {
  var that = this
   var  user_info_token  = wx.getStorageSync('token');
   //获取缓存中的openid或者是全局当中的openid变量 通过指定openid查询后台的token 
   wx.request({
    url: 'https://vip.join.ink/api/get_token',
    data: {
      openid:wx.getStorageSync('openid'),  //获取缓存中的openid openid不会变
      token:user_info_token
    },
    method: "GET",
    header: {
      'content-type': 'application/json' // 默认值
    },
    success (res) {
      console.log(res.data)
      if(res.data.code ==200){
          //此时重新设置新的token作为缓存对象
          wx.setStorageSync('token', res.data.api_token)
          wx.setStorageSync('token_is_agreement',res.data.data) //一致情况下为1
      }else{
        //缓存中的token和后台当中的token不一致  需要重新授权登录
        wx.removeStorageSync('token')
        if(user_info_token !=''){
          wx.showModal({
            title: '提示',
            content: 'token失效，请重新登录！',
            showCancel: false
          })
          setTimeout(function() {
            wx.switchTab({
              url: '/pages/personal/personal',
            })
         }, 2000);
        }

      }
    }
  })
},
  // 收藏活动
  activity_collection(){
      var that = this
      let activity_id = that.data.act_info.id
      app.request("/user/add_activity_Favorites", {
        method: "POST",
        data: {
          activity_id: activity_id,
          // store_id,
        }
      }).then(res => {
        console.log(res, "收藏活动");
        if (res.data.code == 200) {
          that.setData({
           
          })
          wx.showToast({
            title: '收藏成功',
          })
        }else{
          that.setData({
           
          })
          wx.showToast({
            title: '取消收藏成功',
          })
        }
        
      })
  
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log(res)
      },
      fail(e) {
        console.log(e)
      }
    })
    console.log(options);
    let id = options.id
    this.fetch(id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     var  that  = this
     that.check_token();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

 /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this
    console.log(res, '88888888888888888888')
    if (res.from === 'button') {
      console.log("来自页面内转发按钮");
      console.log(res.target);
    } else {
      var pages = getCurrentPages() //获取加载的页面
      var currentPage = pages[pages.length-1] //获取当前页面的对象
      var url = currentPage.route //当前页面url
      console.log(url,'888888888888')
      console.log("来自右上角转发菜单")
      return {
         title:  '分享至朋友圈',
         path: url,
        success: (res) => {
          console.log("转发成功", res);
        },
        fail: (res) => {
          console.log("转发失败", res);
        }
      }
    }

  },
})